<?php

/*
	info: 签到
	date: 2011-12-21
*/

define('PAGESCRIPT', 'checkin');
require dirname(__FILE__).'/include/common.inc.php';
$webglobal['file_checkin'] = array();

if ($webglobal['action'] == 'view') {

} else {
	$companyCommentFiles = array('`tm_user`.`nickname` as personname', '`tm_user`.`avatar` as avatar', '`tm_company`.`id` as company_id', '`tm_company`.`name` as companyname', '`tm_company_comment`.id','`tm_company_comment`.`photo`', '`tm_company_comment`.title','`tm_company_comment`.`content`', '`tm_company_comment`.inquiry_title','`tm_company_comment`.inquiry_result', '`tm_company_comment`.`floor`','`tm_company_comment`.ref_id', '`tm_company_comment`.`score`','`tm_company_comment`.`addtime`', '`tm_company_comment`.`status`', '`tm_company_comment`.`photo`', '`tm_company_comment`.user_type'
	);
	$webglobal['file_index']['companyComment'] = array();
	$fileSql = implode(',', $companyCommentFiles);

	$tempsearchsql = ' WHERE `tm_company_comment`.`status` = 1';
	if ($webglobal['curcityid'] > 0) {
		$tempsearchsql .= ' AND `tm_company`.city = ' . $webglobal['curcityid'];
	}

	$webrequest['page'] = isset($webrequest['page']) ? max(1, intval($webrequest['page'])) : 1;
	$webglobal['file_checkin']['perpage'] = 10;
	$webglobal['file_checkin']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}company_comment LEFT JOIN `tm_company` ON `tm_company_comment`.`company_id` = `tm_company`.`id`{$tempsearchsql}");

	$webglobal['file_companyComment'] = array();
	if ($webglobal['file_checkin']['datanum'] > 0) {
		$option = array(
			'page' => '@@'
		);

		$webglobal['page'] = global_Page($webglobal['file_checkin']['datanum'], $webglobal['file_checkin']['perpage'], $webrequest['page'], buildURLForModule('checkin', $option));
		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_checkin']['perpage'];

		$query = $db->Query("SELECT {$fileSql} FROM `tm_company_comment` LEFT JOIN `tm_company` ON `tm_company_comment`.`company_id` = `tm_company`.`id` LEFT JOIN `tm_user` ON `tm_company_comment`.`user_id` = `tm_user`.`id`{$tempsearchsql} ORDER BY `tm_company_comment`.addtime DESC LIMIT $tempstartnum, {$webglobal['file_checkin']['perpage']}");
		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['content'] = preg_replace("/\{:(.*?):\}/ies", "replaceImage('\\1')", $tempdata['content']);
			$tempdata['addtime'] =  !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
			$webglobal['file_companyComment'][$tempdata['id']] = $tempdata;
		}
	}

	list($year, $month, $day) = explode('-', global_FormatTime($webserver['timestamp']));

	$todayTime = global_ConvTime(global_FormatTime($webserver['timestamp'], 'FD') . ' 00:00:00');
	$yesterDayTime = $todayTime - 86400;
	$groupLimit = 5;

	//$filter = " WHERE addtime >= {$yesterDayTime} AND addtime < {$todayTime}";
	$filter = "WHERE `tm_company_comment`.status = 1";
	if ($webglobal['curcityid'] > 0) {
		$filter .= ' AND `tm_company`.city = ' . $webglobal['curcityid'];
	}

	$goroupSql = "SELECT `tm_company_comment`.company_id, COUNT(`tm_company_comment`.company_id) as countNum FROM `tm_company_comment` LEFT JOIN `tm_company` ON `tm_company_comment`.`company_id` = `tm_company`.`id`{$filter} GROUP BY `tm_company_comment`.company_id ORDER BY countNum DESC LIMIT 0, $groupLimit";

	$webglobal['file_companyComment_group'] = array();
	$companyId = array();
	$groupdata = $db->Query($goroupSql);
	while ($tempdata = $db->FetchArray($groupdata)) {
		
		$companyId[] = $tempdata['company_id'];
		$tempdata['addtime'] =  !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
		$webglobal['file_companyComment_group'][$tempdata['company_id']] = $tempdata;
	}

	//查询商家信息
	if ($companyId) {
		$companySql = "SELECT id, name, image FROM `tm_company` WHERE id IN (". implode(',', $companyId) .")";
		$query = $db->Query($companySql);
		while ($tempdata = $db->FetchArray($query)) {
			if (isset($webglobal['file_companyComment_group'][$tempdata['id']])) {
				$webglobal['file_companyComment_group'][$tempdata['id']]['name'] = $tempdata['name'];
				$webglobal['file_companyComment_group'][$tempdata['id']]['image'] = $tempdata['image'];
			}
		}
	}
}

function replaceImage($imageTitle) {
	if ($imageTitle != '') {
		return '<img src="'. WEBURLPATH . '/images/emoji/' . $imageTitle . '.png' .'" />';
	}
	return $imageTitle;
}

template_Display('checkin');
